library(tidyverse) # data manipulation
library(ggpubr) # producing data exploratory plots
library(modelsummary) # descriptive data
library(glmmTMB) # running generalised mixed models
library(DHARMa) # model diagnostics
library(performance) # model diagnostics
library(ggeffects) # partial effect plots
library(car) # running Anova on model
library(emmeans) # post-hoc analysisdf_adults_cleaned <- df_adults |>
mutate(FISH_ID = factor(FISH_ID),
Sex = factor(Sex),
Population = factor(Population),
Tank = factor(Tank),
Chamber = factor(Chamber),
System =factor(System),
Temperature =factor(Temperature),
True_resting=factor(True_resting))
df_males <- df_adults_cleaned |>
filter(Sex == "M")
df_females <- df_adults_cleaned |>
filter(Sex == "F")
df_adults_cleaned2 <- df_males |>
full_join(select(df_females, c("Tank","Temperature","Mass","Resting","Max","AAS","FISH_ID","Sex")), by="Tank") |>
mutate(Temperature.x = coalesce(Temperature.x, Temperature.y),
FISH_ID.x = coalesce(FISH_ID.x, FISH_ID.y),
Sex.x = coalesce(Sex.x, Sex.y)) df_jresp$Population <- fct_collapse(df_jresp$Population,
`Vlassof cay`= c("Vlassof reef", "Vlassof", "Vlassof Cay", "Vlassof cay"),
`Arlington reef` = c("Arlington reef","Arlginton reef"))
#df_jresp$Female <- fct_collapse(df_jresp$Female,
#`CARL359`= c("CARL359", "CARL59"))
df_jresp2 <- df_jresp |>
unite("F0", c("Male","Female"), sep="_", remove=FALSE) |>
mutate(across(1:7, factor),
Temperature = factor(Temperature),
True_resting = factor(True_resting))
#df_jresp2_rest <- df_jresp2 |>
#filter(True_resting == "Y")temp2a <- temp1a |>
left_join(select(df_adults_cleaned2, c("FISH_ID.x",
"Sex.x",
"Resting.x",
"Max.x",
"AAS.x",
"Mass.x")),
by="FISH_ID.x")temp2b <- temp1b |>
left_join(select(df_adults_cleaned2, c("FISH_ID.y",
"Sex.y",
"Resting.y",
"Max.y",
"AAS.y",
"Mass.y")),
by="FISH_ID.y") df_merged <- temp2a |>
left_join(select(temp2b, c("Clutch","Replicate",
"FISH_ID.y",
"Resting.y",
"Max.y",
"AAS.y",
"Mass.y")),
by=c("Clutch","Replicate"))df <- df_merged |>
mutate(Resting_MALE =Resting.x,
Max_MALE =Max.x,
AAS_MALE =AAS.x,
Mass_MALE =Mass.x,
FISH_ID.y =FISH_ID.x,#makes more sense for males to be .y instead of .x
FISH_ID.x =FISH_ID.x,
Resting_FEMALE =Resting.y,
Max_FEMALE =Max.y,
AAS_FEMALE =AAS.y,
Mass_FEMALE =Mass.y) |>
mutate(AAS_MALE = AAS_MALE/Mass_MALE,
AAS_FEMALE =AAS_FEMALE/Mass_FEMALE) |>
mutate(AAS_MID =(AAS_MALE+AAS_FEMALE)/2) |> # easier to do it again
filter(True_resting == "Y") |> # easier to do it again
mutate(AAS_MID =coalesce(AAS_MID, AAS_MALE)) |>
mutate(AAS_MID =coalesce(AAS_MID, AAS_FEMALE)) |>
drop_na(AAS) |>
group_by(Clutch) |>
mutate(MEDIAN_AAS =median(AAS_kg_wet)) |>
ungroup() |>
select(-c(Replicate, Chamber, System, Volume, Date_tested, Swim, Mass, Dry_mass, 18:26)) |>
distinct() |>
drop_na(AAS_MID)plot <- ggplot(df, aes(x=AAS_MALE, y=MEDIAN_AAS, color=Temperature)) +
stat_smooth(method = "lm") +
#geom_point(alpha=0.1) +
ggtitle("Offspring-male relationship") +
xlab("AAS (offspring)") +
ylab("AAS (parental-male)") +
theme_classic() +
theme(legend.position = 'right')
plotplot <- ggplot(df, aes(x=AAS_MID, y=MEDIAN_AAS, color=Temperature)) +
stat_smooth(method = "lm") +
#geom_point(alpha=0.1) +
ggtitle("Offspring-midpoint relationship") +
xlab("AAS (offspring)") + ylab("AAS (parental-midpoint)") +
theme_classic() +
theme(legend.position = 'right')
plot| Population | 27 | 28.5 | 30 |
|---|---|---|---|
| Arlington reef | 9 | 6 | 3 |
| Pretty patches | 4 | 3 | 5 |
| Sudbury reef | 4 | 2 | 2 |
| Vlassof cay | 4 | 0 | 4 |
| F0 | 27 | 28.5 | 30 |
|---|---|---|---|
| CARL217_CARL226 | 0 | 1 | 0 |
| CARL218_CARL222 | 0 | 0 | 2 |
| CARL230_CARL235 | 2 | 0 | 0 |
| CARL233_CARL215 | 0 | 0 | 0 |
| CARL237_CARL219 | 2 | 0 | 0 |
| CARL241_CARL239 | 2 | 0 | 0 |
| CARL249_CARL360 | 0 | 0 | 1 |
| CARL335_CARL359 | 0 | 2 | 0 |
| CARL338_CARL345 | 0 | 1 | 0 |
| CARL344_CARL370 | 0 | 0 | 0 |
| CARL354_CARL355 | 3 | 0 | 0 |
| CARL360_CARL249 | 0 | 0 | 0 |
| CARL367_CARL363 | 0 | 1 | 0 |
| CARL369_CARL349 | 0 | 1 | 0 |
| CPRE189_CPRE202 | 0 | 0 | 2 |
| CPRE372_CPRE209 | 1 | 0 | 0 |
| CPRE372_CPRE370 | 1 | 0 | 0 |
| CPRE375_CPRE377 | 2 | 0 | 0 |
| CPRE391_CPRE390 | 0 | 0 | 1 |
| CPRE447_CPRE452 | 0 | 0 | 2 |
| CPRE453_CPRE459 | 0 | 1 | 0 |
| CPRE521_CPRE524 | 0 | 1 | 0 |
| CPRE550_CPRE533 | 0 | 1 | 0 |
| CSUD002_CSUD213 | 0 | 1 | 0 |
| CSUD009_CSUD212 | 2 | 0 | 0 |
| CSUD013_CSUD017 | 2 | 0 | 0 |
| CSUD016_CSUD078 | 0 | 1 | 0 |
| CSUD312_CSUD304 | 0 | 0 | 2 |
| CVLA049_CVLA098 | 0 | 0 | 0 |
| CVLA089_CVLA059 | 0 | 0 | 1 |
| CVLA102_CVLA466 | 1 | 0 | 0 |
| CVLA106_CVLA091 | 0 | 0 | 2 |
| CVLA468_CVLA477 | 2 | 0 | 0 |
| CVLA486_CVLA463 | 1 | 0 | 0 |
| CVLA498_CVLA493 | 0 | 0 | 1 |
| Temperature | NUnique | mean | median | min | max | sd | Histogram |
|---|---|---|---|---|---|---|---|
| 27 | 21 | 584.54 | 578.01 | 421.02 | 793.67 | 91.11 | ▁▃▄▄▇▄▃▃ |
| 28.5 | 11 | 699.55 | 711.16 | 514.83 | 826.07 | 101.40 | ▃▃▃▃▇▃▇▇ |
| 30 | 14 | 767.46 | 718.59 | 597.88 | 1022.17 | 147.90 | ▇▇▅▂▂▂▂▅ |
| Population | 27 | 28.5 | 30 |
|---|---|---|---|
| Arlington reef | 8 | 7 | 4 |
| Pretty patches | 4 | 6 | 4 |
| Sudbury reef | 4 | 3 | 2 |
| Vlassof cay | 6 | 2 | 5 |
datasummary(Factor(Population) ~ Factor(Temperature)*Factor(Sex),
data = df_adults_cleaned,
fmt = "%.0f")| 27 | 28.5 | 30 | ||||
|---|---|---|---|---|---|---|
| Population | F | M | F | M | F | M |
| Arlington reef | 4 | 4 | 2 | 5 | 2 | 2 |
| Pretty patches | 2 | 2 | 3 | 3 | 3 | 1 |
| Sudbury reef | 2 | 2 | 1 | 2 | 1 | 1 |
| Vlassof cay | 3 | 3 | 1 | 1 | 3 | 2 |
Pairs
datasummary(Factor(Population)*Factor(Temperature.x) ~ AAS.x*(NUnique),
data = df_adults_cleaned2,
fmt = "%.0f")| Population | Temperature.x | NUnique |
|---|---|---|
| Arlington reef | 27 | 4 |
| 28.5 | 5 | |
| 30 | 2 | |
| Pretty patches | 27 | 2 |
| 28.5 | 3 | |
| 30 | 1 | |
| Sudbury reef | 27 | 2 |
| 28.5 | 2 | |
| 30 | 1 | |
| Vlassof cay | 27 | 3 |
| 28.5 | 1 | |
| 30 | 2 |
| Temperature | NUnique | mean | median | min | max | sd | Histogram |
|---|---|---|---|---|---|---|---|
| 27 | 22 | 10.29 | 10.26 | 3.85 | 16.28 | 3.14 | ▃▁▄▇▃▆▃▃▁ |
| 28.5 | 18 | 10.59 | 9.66 | 6.11 | 20.44 | 3.66 | ▅▅▇▇▃▂▂ |
| 30 | 15 | 9.19 | 9.16 | 4.36 | 12.77 | 2.91 | ▃▂▅▂▂▂▃▇ |
After figuring out which random factors will be incorporated into the model we will start to examine out fixed factors. Some fixed factors such as AAS_(FE)MALE and TEMPERATURE will be essential to answering questions we have around heritability. Another factor that will be included is Dry_mass - which it should be pointed out in this experiment refers to the mass of fish after they were blotted dry with paper towel rather than completely dried out. Larger fish consume more oxygen, therefore, we need to account for this known relationship within our model. Out model will look something like this:
If we had alternative hypotheses to test would would do so at this stage. But in this instance the experiment was designed to answer a specific question via limiting potential covariates.
Great now lets check how out model performed via model validation techniques
To check out model performance we will be using two different packages that perform model diagnositics. The packages used here are just examples, there are other packages out there that can provide the same function.
## Object of Class DHARMa with simulated residuals based on 250 simulations with refit = FALSE . See ?DHARMa::simulateResiduals for help.
##
## Scaled residual values: 0.192 0.448 0.752 0.212 0.108 0.04 0.216 0.44 0.132 0.052 0.284 0.768 0.568 0.108 0.404 0.62 0.572 0.128 0.984 0.768 ...
## $uniformity
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: simulationOutput$scaledResiduals
## D = 0.091619, p-value = 0.8724
## alternative hypothesis: two-sided
##
##
## $dispersion
##
## DHARMa nonparametric dispersion test via sd of residuals fitted vs.
## simulated
##
## data: simulationOutput
## dispersion = 1.0291, p-value = 0.84
## alternative hypothesis: two.sided
##
##
## $outliers
##
## DHARMa outlier test based on exact binomial test with approximate
## expectations
##
## data: simulationOutput
## outliers at both margin(s) = 0, observations = 42, p-value = 1
## alternative hypothesis: true probability of success is not equal to 0.007968127
## 95 percent confidence interval:
## 0.00000000 0.08408385
## sample estimates:
## frequency of outliers (expected: 0.00796812749003984 )
## 0
## $uniformity
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: simulationOutput$scaledResiduals
## D = 0.091619, p-value = 0.8724
## alternative hypothesis: two-sided
##
##
## $dispersion
##
## DHARMa nonparametric dispersion test via sd of residuals fitted vs.
## simulated
##
## data: simulationOutput
## dispersion = 1.0291, p-value = 0.84
## alternative hypothesis: two.sided
##
##
## $outliers
##
## DHARMa outlier test based on exact binomial test with approximate
## expectations
##
## data: simulationOutput
## outliers at both margin(s) = 0, observations = 42, p-value = 1
## alternative hypothesis: true probability of success is not equal to 0.007968127
## 95 percent confidence interval:
## 0.00000000 0.08408385
## sample estimates:
## frequency of outliers (expected: 0.00796812749003984 )
## 0
## Family: gaussian ( identity )
## Formula: MEDIAN_AAS ~ scale(AAS_MALE) * Temperature
## Data: df
##
## AIC BIC logLik deviance df.resid
## 525.5 537.7 -255.7 511.5 35
##
##
## Dispersion estimate for gaussian family (sigma^2): 1.14e+04
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 589.17 23.92 24.632 < 2e-16 ***
## scale(AAS_MALE) 24.77 29.21 0.848 0.39655
## Temperature28.5 116.23 41.03 2.833 0.00461 **
## Temperature30 213.49 41.45 5.151 2.59e-07 ***
## scale(AAS_MALE):Temperature28.5 -46.44 43.52 -1.067 0.28593
## scale(AAS_MALE):Temperature30 -36.91 40.11 -0.920 0.35735
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 2.5 % 97.5 % Estimate
## (Intercept) 542.28902 636.05054 589.16978
## scale(AAS_MALE) -32.48823 82.02028 24.76602
## Temperature28.5 35.81707 196.65283 116.23495
## Temperature30 132.25704 294.72983 213.49344
## scale(AAS_MALE):Temperature28.5 -131.73997 38.85823 -46.44087
## scale(AAS_MALE):Temperature30 -115.51948 41.69145 -36.91402
model1.1 |> emmeans(pairwise ~ Temperature, type="response") |>
summary(by=NULL, adjust="sidak", infer=TRUE)## NOTE: Results may be misleading due to involvement in interactions
## $emmeans
## Temperature emmean SE df lower.CL upper.CL t.ratio p.value
## 27 589 23.9 35 529 649 24.632 <.0001
## 28.5 705 33.3 35 622 789 21.160 <.0001
## 30 803 33.8 35 718 888 23.713 <.0001
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 3 estimates
## P value adjustment: sidak method for 3 tests
##
## $contrasts
## contrast estimate SE df lower.CL upper.CL t.ratio
## Temperature27 - Temperature28.5 -116.2 41.0 35 -219 -13.4 -2.833
## Temperature27 - Temperature30 -213.5 41.4 35 -317 -109.6 -5.151
## Temperature28.5 - Temperature30 -97.3 47.5 35 -216 21.9 -2.047
## p.value
## 0.0226
## <.0001
## 0.1378
##
## Confidence level used: 0.95
## Conf-level adjustment: sidak method for 3 estimates
## P value adjustment: sidak method for 3 tests
om.aas <- emmeans(model1.1, ~AAS_MALE*Temperature,
at =list(AAS_MALE=seq(from=100, to =400, by=5)))
om.aas.df <- as.data.frame(om.aas)
om.aas.obs <- drop_na(df, AAS_MALE, MEDIAN_AAS) |>
mutate(Pred =predict(model1.1, re.form =NA, type='response'),
Resid =residuals(model1.1, type ="response"),
Fit =Pred + Resid)
om.aas.obs.summarize <- om.aas.obs |>
group_by(Clutch, Temperature) |>
summarise(mean.aas =mean(Fit, na.rm=TRUE),
mean.aas_male =mean(AAS_MALE, na.rm=TRUE),
sd.aas =sd(Fit, na.rm =TRUE),
n.aas = n()) |>
mutate(se.aas = sd.aas / sqrt(n.aas),
lower.ci.aas =mean.aas - qt(1 - (0.05/2), n.aas -1) * se.aas,
upper.ci.aas =mean.aas + qt(1 - (0.05/2), n.aas - 1) * se.aas)|>
ungroup()## `summarise()` has grouped output by 'Clutch'. You can override using the
## `.groups` argument.
## Warning: There were 84 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `lower.ci.aas = mean.aas - qt(1 - (0.05/2), n.aas - 1) *
## se.aas`.
## ℹ In group 1: `Clutch = 38`.
## Caused by warning in `qt()`:
## ! NaNs produced
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 83 remaining warnings.
ggplot(data =om.aas.df, aes(y=emmean, x=AAS_MALE)) +
stat_smooth(aes(color=Temperature),
method = "lm") +
geom_pointrange(data = om.aas.obs.summarize, aes(y =mean.aas, x=mean.aas_male,
ymin =lower.ci.aas,
ymax =upper.ci.aas, color = Temperature),
alpha =0.2) +
facet_wrap(~Temperature) +
theme_classic() +
theme(legend.position ="bottom")## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 21 rows containing missing values or values outside the scale range
## (`geom_segment()`).
## Warning: Removed 11 rows containing missing values or values outside the scale range
## (`geom_segment()`).
## Warning: Removed 10 rows containing missing values or values outside the scale range
## (`geom_segment()`).
Great now lets check how out model performed via model validation techniques
To check out model performance we will be using two different packages that perform model diagnositics. The packages used here are just examples, there are other packages out there that can provide the same function.
## Object of Class DHARMa with simulated residuals based on 250 simulations with refit = FALSE . See ?DHARMa::simulateResiduals for help.
##
## Scaled residual values: 0.224 0.464 0.744 0.296 0.12 0.144 0.036 0.16 0.024 0.46 0.208 0.04 0.272 0.808 0.708 0.188 0.368 0.588 0.576 0.592 ...
## $uniformity
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: simulationOutput$scaledResiduals
## D = 0.080348, p-value = 0.9278
## alternative hypothesis: two-sided
##
##
## $dispersion
##
## DHARMa nonparametric dispersion test via sd of residuals fitted vs.
## simulated
##
## data: simulationOutput
## dispersion = 1.0232, p-value = 0.856
## alternative hypothesis: two.sided
##
##
## $outliers
##
## DHARMa outlier test based on exact binomial test with approximate
## expectations
##
## data: simulationOutput
## outliers at both margin(s) = 0, observations = 46, p-value = 1
## alternative hypothesis: true probability of success is not equal to 0.007968127
## 95 percent confidence interval:
## 0.00000000 0.07706183
## sample estimates:
## frequency of outliers (expected: 0.00796812749003984 )
## 0
## $uniformity
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: simulationOutput$scaledResiduals
## D = 0.080348, p-value = 0.9278
## alternative hypothesis: two-sided
##
##
## $dispersion
##
## DHARMa nonparametric dispersion test via sd of residuals fitted vs.
## simulated
##
## data: simulationOutput
## dispersion = 1.0232, p-value = 0.856
## alternative hypothesis: two.sided
##
##
## $outliers
##
## DHARMa outlier test based on exact binomial test with approximate
## expectations
##
## data: simulationOutput
## outliers at both margin(s) = 0, observations = 46, p-value = 1
## alternative hypothesis: true probability of success is not equal to 0.007968127
## 95 percent confidence interval:
## 0.00000000 0.07706183
## sample estimates:
## frequency of outliers (expected: 0.00796812749003984 )
## 0
## Family: gaussian ( identity )
## Formula: MEDIAN_AAS ~ scale(AAS_MID) * Temperature
## Data: df
##
## AIC BIC logLik deviance df.resid
## 575.3 588.1 -280.6 561.3 39
##
##
## Dispersion estimate for gaussian family (sigma^2): 1.17e+04
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 583.47 23.62 24.703 < 2e-16 ***
## scale(AAS_MID) 15.77 23.73 0.665 0.50621
## Temperature28.5 116.20 40.58 2.864 0.00419 **
## Temperature30 194.78 38.82 5.017 5.24e-07 ***
## scale(AAS_MID):Temperature28.5 -16.40 35.86 -0.457 0.64750
## scale(AAS_MID):Temperature30 25.98 48.01 0.541 0.58848
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 2.5 % 97.5 % Estimate
## (Intercept) 537.18003 629.76610 583.47307
## scale(AAS_MID) -30.73158 62.27672 15.77257
## Temperature28.5 36.66997 195.73382 116.20190
## Temperature30 118.68956 270.86554 194.77755
## scale(AAS_MID):Temperature28.5 -86.68314 53.88895 -16.39710
## scale(AAS_MID):Temperature30 -68.12866 120.08465 25.97799
om.aas <- emmeans(mid_model1.1, ~AAS_MID*Temperature,
at =list(AAS_MID =seq(from=100, to =450, by=5)))
om.aas.df <- as.data.frame(om.aas)
om.aas.obs <- drop_na(df, AAS_MID, MEDIAN_AAS) |>
mutate(Pred =predict(mid_model1.1, re.form =NA, type='response'),
Resid =residuals(mid_model1.1, type ="response"),
Fit =Pred + Resid)
om.aas.obs.summarize <- om.aas.obs |>
group_by(Clutch, Temperature) |>
summarise(mean.aas =mean(Fit, na.rm=TRUE),
mean.aas_female =mean(AAS_MID, na.rm=TRUE),
sd.aas =sd(Fit, na.rm =TRUE),
n.aas = n()) |>
mutate(se.aas = sd.aas / sqrt(n.aas),
lower.ci.aas =mean.aas - qt(1 - (0.05/2), n.aas -1) * se.aas,
upper.ci.aas =mean.aas + qt(1 - (0.05/2), n.aas - 1) * se.aas)|>
ungroup()## `summarise()` has grouped output by 'Clutch'. You can override using the
## `.groups` argument.
## Warning: There were 92 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `lower.ci.aas = mean.aas - qt(1 - (0.05/2), n.aas - 1) *
## se.aas`.
## ℹ In group 1: `Clutch = 38`.
## Caused by warning in `qt()`:
## ! NaNs produced
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 91 remaining warnings.
ggplot(data =om.aas.df, aes(y=emmean, x=AAS_MID)) +
stat_smooth(aes(color=Temperature),
method = "lm") +
geom_pointrange(data = om.aas.obs.summarize, aes(y =mean.aas, x=mean.aas_female,
ymin =lower.ci.aas,
ymax =upper.ci.aas, color = Temperature),
alpha =0.2) +
facet_wrap(~Temperature) +
theme_classic() +
theme(legend.position ="bottom")## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 21 rows containing missing values or values outside the scale range
## (`geom_segment()`).
## Warning: Removed 11 rows containing missing values or values outside the scale range
## (`geom_segment()`).
## Warning: Removed 14 rows containing missing values or values outside the scale range
## (`geom_segment()`).